﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using MapWindow.Data;
using MapWindow.Drawing;
using MapWindow.Map;
namespace Build_A_Map
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        // lines - fs.Open(@"C:\dev\MapWindow6Dev\Tutorial\Components\Ex3\Data\MajorRoads.shp");
        // polygon - fs.Open(@"C:\dev\MapWindow6Dev\Tutorial\Components\Ex3\Data\MajorBoundaries.shp");
        protected override void OnShown(EventArgs e)
        {
            OpenRaster();
            base.OnShown(e);
        }

        private void OpenVector()
        {
            FeatureSet fs = new FeatureSet();
            fs.Open(@"C:\dev\MapWindow6Dev\Tutorial\Components\Ex3\Data\MajorBoundaries.shp");
            map1.Layers.Add(fs);
        }

        private void OpenRaster()
        {
            Raster r = new Raster();
            r.Open(@"C:\dev\MapWindow6Dev\Tutorial\Components\Ex3\Data\DEM\Elevation.bgd");
            r.NoDataValue = 0;
            for (int row = 0; row < r.NumRows; row++)
            {
                for (int col = 0; col < r.NumColumns; col++)
                {
                    if (r.Value[row, col] > 600) r.Value[row, col] = 600;
                }
            }
            IMapRasterLayer myLayer = map1.Layers.Add(r);
            Quantile(myLayer);
        }

        private void Quantile(IMapRasterLayer myLayer)
        {
            myLayer.Symbolizer.EditorSettings.IntervalMethod = IntervalMethods.Quantile;
            myLayer.Symbolizer.EditorSettings.NumBreaks = 5;
            myLayer.Symbolizer.Scheme.CreateCategories(myLayer.DataSet);
            myLayer.Symbolizer.ShadedRelief.ElevationFactor = .002f;
            myLayer.Symbolizer.ShadedRelief.IsUsed = true;
            myLayer.WriteBitmap();
        }

        private void Glaciers(IMapRasterLayer myLayer)
        {
            myLayer.Symbolizer.Scheme.ApplyScheme(ColorSchemes.Glaciers, myLayer.DataSet);
            myLayer.Symbolizer.Scheme.Categories[0].Range = new Range(1, 150);
            myLayer.Symbolizer.Scheme.Categories[0].ApplyMinMax(myLayer.Symbolizer.EditorSettings);
            myLayer.Symbolizer.Scheme.Categories[1].Range = new Range(150, 800);
            myLayer.Symbolizer.Scheme.Categories[1].ApplyMinMax(myLayer.Symbolizer.EditorSettings);
            myLayer.Symbolizer.ShadedRelief.ElevationFactor = 1;
            myLayer.Symbolizer.ShadedRelief.IsUsed = true;
            
            myLayer.WriteBitmap();
        }

        private void AddLighting(IMapRasterLayer myLayer)
        {
            myLayer.Symbolizer.Scheme.Categories[0].Range = new Range(1, 150);
            myLayer.Symbolizer.Scheme.Categories[0].ApplyMinMax(myLayer.Symbolizer.EditorSettings);
            myLayer.Symbolizer.Scheme.Categories[1].Range = new Range(150, 800);
            myLayer.Symbolizer.Scheme.Categories[1].ApplyMinMax(myLayer.Symbolizer.EditorSettings);
            myLayer.Symbolizer.ShadedRelief.ElevationFactor = 1;
            myLayer.Symbolizer.ShadedRelief.IsUsed = true;
            myLayer.WriteBitmap();
        }

        private void ControlRange(IMapRasterLayer myLayer)
        {
            myLayer.Symbolizer.Scheme.Categories[0].Range = new Range(1, 150);
            myLayer.Symbolizer.Scheme.Categories[0].ApplyMinMax(myLayer.Symbolizer.EditorSettings);
            myLayer.Symbolizer.Scheme.Categories[1].Range = new Range(150, 800);
            myLayer.Symbolizer.Scheme.Categories[1].ApplyMinMax(myLayer.Symbolizer.EditorSettings);
            myLayer.WriteBitmap();
        }


        private void Labels(IMapFeatureLayer myLayer)
        {
            IMapLabelLayer labelLayer = new MapLabelLayer();
            ILabelCategory category = labelLayer.Symbology.Categories[0];
            category.Expression = "[nam]\nID: [Cnt_nam]";
            category.FilterExpression = "[nam] = 'Tasmania' OR [nam] = 'Queensland'";
            category.Symbolizer.BackColorEnabled = true;
            category.Symbolizer.BackColor = Color.FromArgb(128, Color.LightBlue);
            category.Symbolizer.BorderVisible = true;
            category.Symbolizer.FontStyle = FontStyle.Bold;
            category.Symbolizer.FontColor = Color.DarkRed;
            category.Symbolizer.Orientation = ContentAlignment.MiddleCenter;
            category.Symbolizer.Alignment = StringAlignment.Center;
            myLayer.ShowLabels = true;
            myLayer.LabelLayer = labelLayer;
        }

        private void MultiplePatterns(IMapFeatureLayer myLayer)
        {
            PolygonSymbolizer mySymbolizer = new PolygonSymbolizer();
            mySymbolizer.Patterns.Add(new HatchPattern(HatchStyle.WideDownwardDiagonal, Color.Red, Color.Transparent));
            myLayer.Symbolizer = mySymbolizer;
        }

        private void CustomCategories(IMapFeatureLayer myLayer)
        {
            PolygonScheme scheme = new PolygonScheme();
            PolygonCategory queensland = new PolygonCategory(Color.LightBlue, Color.DarkBlue, 1);
            queensland.FilterExpression = "[nam] = 'Queensland'";
            queensland.LegendText = "Queensland";

            PolygonCategory nWords = new PolygonCategory(Color.Pink, Color.DarkRed, 1);
            nWords.FilterExpression = "[nam] Like 'N*'";
            nWords.LegendText = "N - Words";
            scheme.ClearCategories();
            scheme.AddCategory(queensland);
            scheme.AddCategory(nWords);
            myLayer.ShowLabels = true;
            myLayer.Symbology = scheme;

            IMapLabelLayer labelLayer = new MapLabelLayer();
            ILabelCategory category = new LabelCategory();
            category.FilterExpression = "[nam] Like 'N*'";
            category.Expression = "[nam]";
            category.Symbolizer.BackColorEnabled = false;
            category.Symbolizer.BorderVisible = false;
            category.Symbolizer.Orientation = ContentAlignment.MiddleCenter;
            labelLayer.Symbology.Categories.Clear();
            labelLayer.Symbology.Categories.Add(category);
            myLayer.LabelLayer = labelLayer;
            

         
           
         
        }

        private void UniqueCoolGradients(IMapFeatureLayer myLayer)
        {
            PolygonScheme myScheme = new PolygonScheme();
            myScheme.EditorSettings.StartColor = Color.LightGreen;
            myScheme.EditorSettings.EndColor = Color.LightBlue;
            myScheme.EditorSettings.ClassificationType = ClassificationTypes.UniqueValues;
            myScheme.EditorSettings.FieldName = "nam";
            myScheme.EditorSettings.UseGradient = true;
            myScheme.CreateCategories(myLayer.DataSet.DataTable);
            myLayer.Symbology = myScheme;
        }


        private void ShapeSpecificBlueGradient(IMapFeatureLayer myLayer)
        {
            PolygonSymbolizer blueGradient = new PolygonSymbolizer(Color.LightSkyBlue, Color.DarkBlue, -45, GradientTypes.Linear);
            blueGradient.SetOutline(Color.Yellow, 1);
            PolygonScheme myScheme = new PolygonScheme();
            myScheme.EditorSettings.TemplateSymbolizer = blueGradient;
            myScheme.EditorSettings.UseColorRange = false;
            myScheme.EditorSettings.ClassificationType = ClassificationTypes.UniqueValues;
            myScheme.EditorSettings.FieldName = "nam";
            myScheme.EditorSettings.UseGradient = false;
            myScheme.CreateCategories(myLayer.DataSet.DataTable);
            myLayer.Symbology = myScheme;
        }

        private void BlueGradient(IMapFeatureLayer myLayer)
        {
            PolygonSymbolizer blueGradient = new PolygonSymbolizer(Color.LightSkyBlue, Color.DarkBlue, 45, GradientTypes.Linear);
            blueGradient.SetOutline(Color.Yellow, 1);
            myLayer.Symbolizer = blueGradient;
        }

        private void BluePolygons(IMapFeatureLayer myLayer)
        {
            PolygonSymbolizer lightblue = new PolygonSymbolizer(Color.LightBlue);
            lightblue.OutlineSymbolizer = new LineSymbolizer(Color.Blue, 1);
            myLayer.Symbolizer = lightblue;
        }

        private void DecoratedLines(IMapFeatureLayer myLayer)
        {
            LineDecoration star = new LineDecoration();
            star.Symbol = new PointSymbolizer(Color.Yellow, PointShapes.Star, 16);
            star.Symbol.SetOutline(Color.Black, 1);
            star.NumSymbols = 1;
            CartographicStroke blueStroke = new CartographicStroke(Color.Blue);
            blueStroke.Decorations.Add(star);
            LineSymbolizer starLine = new LineSymbolizer();
            starLine.Strokes.Clear();
            starLine.Strokes.Add(blueStroke);
            myLayer.Symbolizer = starLine;
           
        }

        private void RailRoads(IMapFeatureLayer myLayer)
        {
            LineSymbolizer mySymbolizer = new LineSymbolizer();
            mySymbolizer.Strokes.Clear();
            CartographicStroke ties = new CartographicStroke(Color.Brown);
            ties.DashPattern = new float[] {1/6f, 2/6f};
            ties.Width = 6;
            ties.EndCap = LineCap.Flat;
            ties.StartCap = LineCap.Flat;
            CartographicStroke rails = new CartographicStroke(Color.DarkGray);
            rails.CompoundArray = new float[]{.15f,.3f,.6f,.75f};
            rails.Width = 6;
            rails.EndCap = LineCap.Flat;
            rails.StartCap = LineCap.Flat;
            mySymbolizer.Strokes.Add(ties);
            mySymbolizer.Strokes.Add(rails);
            
            myLayer.Symbolizer = mySymbolizer;
        }


        private void RoadsCustomTileID(IMapFeatureLayer myLayer)
        {
            LineScheme myScheme = new LineScheme();
            myScheme.Categories.Clear();
            LineCategory low = new LineCategory(Color.Blue, 2);
            low.FilterExpression = "[tile_id] < 36";
            low.LegendText = "Low";
            LineCategory high = new LineCategory(Color.Red, Color.Black, 6, DashStyle.Solid, LineCap.Triangle);
            high.FilterExpression = "[tile_id] >= 36";
            high.LegendText = "High";
            myScheme.AppearsInLegend = true;
            myScheme.LegendText = "Tile ID";
            myScheme.Categories.Add(low);
            myScheme.Categories.Add(high);
            myLayer.Symbology = myScheme;

           
        }

        private void RoadsUniqueTileID(IMapFeatureLayer myLayer)
        {
            LineScheme myScheme = new LineScheme();
            myScheme.EditorSettings.ClassificationType = ClassificationTypes.UniqueValues;
            myScheme.EditorSettings.FieldName = "tile_id";
            myScheme.CreateCategories(myLayer.DataSet.DataTable);
            myLayer.Symbology = myScheme;
        }

        private void YellowRoadsWithBlackOutline(IMapFeatureLayer myLayer)
        {
            LineSymbolizer road = new LineSymbolizer(Color.Yellow, 5);
            road.SetOutline(Color.Black, 1);
            myLayer.Symbolizer = road;
        }

        private void BrownRoads(IMapFeatureLayer myLayer)
        {
            myLayer.Symbolizer = new LineSymbolizer(Color.Brown, 1);
        }

        private void MakeYellowStars(IMapFeatureLayer featureLayer)
        {
            featureLayer.Symbolizer = new PointSymbolizer(Color.Yellow, PointShapes.Star, 16);
        }

        private void MakeOutlinedYellowStars(IMapFeatureLayer featureLayer)
        {
            featureLayer.Symbolizer = new PointSymbolizer(Color.Yellow, PointShapes.Star, 16);
            featureLayer.Symbolizer.SetOutline(Color.Black, 1);
        }

        private void MilitaryPlane(IMapFeatureLayer featureLayer)
        {
            featureLayer.Symbolizer = new PointSymbolizer('M', "Military", Color.Blue, 16);
        }

        private void Tiger(IMapFeatureLayer myLayer)
        {
            myLayer.Symbolizer = new PointSymbolizer(Images.Tiger, 48);
        }

        private void AreaCategories(IMapFeatureLayer myLayer)
        {
            IMapPointLayer myPointLayer = myLayer as IMapPointLayer;
            if(myPointLayer == null) return;
            
            PointScheme myScheme = new PointScheme();
            myScheme.Categories.Clear();
            
            PointCategory smallSize = new PointCategory(Color.Blue, PointShapes.Rectangle, 4);
            smallSize.FilterExpression = "[Area] < .01";
            smallSize.LegendText = "Small Cities";
            myScheme.AddCategory(smallSize);

            PointCategory largeSize = new PointCategory(Color.Yellow, PointShapes.Star, 16);
            largeSize.FilterExpression = "[Area] >= .01";
            largeSize.LegendText = "Large Cities";
            largeSize.Symbolizer.SetOutline(Color.Black, 1);
            myScheme.AddCategory(largeSize);

            myPointLayer.Symbology = myScheme;
        }

        private void QuantileAreaColors(IMapFeatureLayer myLayer)
        {
            IMapPointLayer myPointLayer = myLayer as IMapPointLayer;
            if (myPointLayer == null) return;

            PointScheme myScheme = new PointScheme();
            myScheme.Categories.Clear();

            myScheme.EditorSettings.ClassificationType = ClassificationTypes.Quantities;
            myScheme.EditorSettings.IntervalMethod = IntervalMethods.Quantile;
            myScheme.EditorSettings.IntervalSnapMethod = IntervalSnapMethods.Rounding;
            myScheme.EditorSettings.IntervalRoundingDigits = 5;
            myScheme.EditorSettings.TemplateSymbolizer = new PointSymbolizer(Color.Yellow, PointShapes.Star, 16);
            myScheme.EditorSettings.FieldName = "Area";
            myScheme.CreateCategories(myLayer.DataSet.DataTable);

            myPointLayer.Symbology = myScheme;
        }
        
        private void YellowStarsInBlueCircles(IMapFeatureLayer myLayer)
        {
            PointSymbolizer myPointSymbolizer = new PointSymbolizer(Color.Blue, PointShapes.Ellipse, 16);
            myPointSymbolizer.Symbols.Add(new SimpleSymbol(Color.Yellow, PointShapes.Star, 10));
            myLayer.Symbolizer = myPointSymbolizer;
        }
    }
}
